1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy.
4 : user/classes/class.usertools.inc.php
5 : - Modulgruppe: Benutzerverwaltung
6 : - Beschreibung: BESCHREIBUNG
7 : - Version: 0.2, 03/01/07
8 : - Autor(en): Mirko Pitz <mirko.pitz@mni.fh-giessen.de>
9 : Markus Keim <markus.keim@mni.fh-giessen.de>
10 : +---------------------------------------------------------------------------+
11 : This program is free software; you can redistribute it and/or
12 : modify it under the terms of the GNU General Public License
13 : as published by the Free Software Foundation; either version 2
14 : of the License, or any later version.
15 : +---------------------------------------------------------------------------+
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 : You should have received a copy of the GNU General Public License
21 : along with this program; if not, write to the Free Software
22 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 : +--------------------------------------------------------------------------*/
24 : /**
25 : * In dieser Datei wird die Klasse "UserTools" implementiert.
26 : * @package eStudy.User
27 : * @author Mirko Pitz <mirko.pitz@mni.fh-giessen.de>
28 : * @author Markus Keim <markus.keim@mni.fh-giessen.de>
29 : * @version 0.2, 03/01/07
30 : */
31 : /**
32 : * Sammlung von Funktionen, die in mehreren Sub-Modulen der Benutzerverwaltung
33 : * verwendet werden.
34 : * @package eStudy.User
35 : * @author Mirko Pitz <mirko.pitz@mni.fh-giessen.de>
36 : * @version 0.1, 23/06/04
37 : */
38 1 : class UserTools {
39 : /**
40 : * Einfache Anführungszeichen am Anfang und am Ende eines Strings entfernen
41 : *
42 : * @access public
43 : * @return void
44 : *
45 : */
46 : function stripSingleSlashes($text = '') {
47 0 : if (is_string($text)) {
48 0 : $strLength = strlen($text);
49 0 : if ($strLength > 0) {
50 0 : if ($text[0] == "'") {
51 0 : $text[0] = "";
52 0 : }
53 0 : if ($text[$strLength-1] == "'") {
54 0 : $text[$strLength-1] = "";
55 0 : }
56 0 : }
57 0 : return trim($text);
58 : } else {
59 0 : return $text;
60 : }
61 : }
62 : /**
63 : * Erzeugt eine 3-spaltige Tabellen-Zeile in einem Formular,
64 : * mit einem Label, einem Eingabefeld und einem Hilfelink.
65 : *
66 : * Die Funktion erzeugt aus den vorgegeben Parametern, eine 3-spaltige
67 : * Tabellen-Zeile in einem Formular.
68 : * Art des Eingabefeldes
69 : * 0 - 'text'
70 : * 1 - 'password'
71 : * 2 - 'readonly'
72 : *
73 : * @access public
74 : * @param string $label Spaltenbezeichnung
75 : * @param string $name Feldname
76 : * @param string $value Inhalt des Feldes
77 : * @param int $size Anzeige-Grösse des Feldes
78 : * @param int $maxLength max. Anzahl Zeichen, die in dem Feld erfasst werden können
79 : * @param string $style css-Formatklassen-Bezeichnung
80 : * @param int $type Art des Eingabefeldes
81 : * @param int $helpID ID des zum Feld gehörigen Hilfetextes
82 : * @param int $tabIndex Tabulator-Reihenfolgeposition
83 : * @return string Html-Anweisung
84 : */
85 : function getEditFormField($label, $name, $value = '', $size = 40, $maxLength = 40, $style = 'tableCell', $type = 0, $helpID = 0, $tabIndex = 0) {
86 : //ohne $name kein gültiges Formularfeld
87 0 : if ($name) {
88 : //Typ auswerten
89 0 : $readOnly = "";
90 0 : $inputType = "text";
91 0 : if (is_numeric($type)) {
92 0 : if ($type == 1) {
93 0 : $inputType = "password";
94 0 : } elseif ($type == 2) {
95 0 : $readOnly = "readonly='readonly'";
96 0 : }
97 0 : } elseif (is_array($type)) {
98 0 : if (in_array($name, $type)) {
99 0 : $readOnly = "readonly='readonly'";
100 0 : }
101 0 : } elseif ($type == $name) {
102 0 : $readOnly = "readonly='readonly'";
103 0 : }
104 : //Admins dürfen alles
105 0 : if ($_SESSION['usergroup'] == ADMIN || ($name == "email" && $_SESSION['usergroup'] == ALUMNUS)) {
106 0 : $readOnly = "";
107 0 : }
108 : // root darf seinen Loginnamen nicht ändern
109 0 : if ($name == "loginname" && $_SESSION["username"] == "root" && $value == "root") {
110 0 : $readOnly = "readonly='readonly'";
111 0 : }
112 : //Tabindex korrigieren
113 0 : $tabIndex = $tabIndex+1000;
114 : //Zellenstil abgleichen
115 0 : if (is_array($style)) {
116 0 : if (isset($style[$name])) $style = $style[$name];
117 0 : else $style = $style['tableRow'];
118 0 : }
119 : //Daten für Ausgabe vorbereiten
120 0 : if ($value == "NULL") {
121 0 : $value = "";
122 0 : }
123 0 : if (is_string($value)) {
124 0 : $value = UserTools::stripSingleSlashes($value);
125 0 : }
126 0 : $code = "<tr>\n";
127 0 : $code.= "<td align='right' class='$style'>$label</td>\n";
128 0 : $code.= "<td class='$style'><p class='pForm'><input name='$name' class='input' type='$inputType' id='$name' size='$size' maxlength='$maxLength' value=\"".Data::toHTML($value, false) ."\" tabindex='$tabIndex' $readOnly/></p>\n";
129 0 : $code.= "</td>\n";
130 0 : $code.= "<td class='$style'>\n";
131 0 : $code.= Utilities::helpCode($helpID);
132 0 : $code.= "</td>\n";
133 0 : $code.= "</tr>\n";
134 0 : return $code;
135 : }
136 0 : }
137 : /**
138 : * Erzeugt eine 3-spaltige Tabellen-Zeile in einem Formular,
139 : * mit einem Label, einem Auswahlfeld und einem Hilfelink.
140 : *
141 : * Die Funktion erzeugt aus den vorgegeben Parametern, eine 3-spaltige
142 : * Tabellen-Zeile in einem Formular.
143 : *
144 : * @access public
145 : * @param string $label Spaltenbezeichnung
146 : * @param string $name Feldname
147 : * @param string $value Inhalt des Feldes
148 : * @param int $size Anzeige-Grösse des Feldes
149 : * @param int $options max. Anzahl Zeichen, die in dem Feld erfasst werden können
150 : * @param string $style css-Formatklassen-Bezeichnung
151 : * @param int $helpID ID des zum Feld gehörigen Hilfetextes
152 : * @param int $tabIndex Tabulator-Reihenfolgeposition
153 : * @return string Html-Anweisung
154 : */
155 : function getOptionFormField($label, $name, $value = 0, $options, $style = 'tableCell', $helpID = 0, $tabIndex = 0) {
156 0 : if ($name) {
157 : //Tabindex korrigieren
158 0 : $tabIndex = $tabIndex+1000;
159 : //Zellenstil abgleichen
160 0 : if (is_array($style)) {
161 0 : if (isset($style[$name])) $style = $style[$name];
162 0 : else $style = $style['tableRow'];
163 0 : }
164 0 : $code = "<tr>\n";
165 0 : $code.= "<td align ='right' class='$style'>$label</td>\n";
166 0 : $code.= "<td class='$style'><p class='pForm'><select class='select' name='$name' size='1' id='$name' tabindex='$tabIndex'>\n";
167 0 : foreach($options as $key => $option) {
168 : //Wenn Wert vorliegt, gebe ihn aus, ansonsten überlese ihn
169 0 : if (!empty($option)) {
170 0 : if ($value == $key) {
171 0 : $code.= " <option selected='selected' value='$key'>$option</option>\n";
172 0 : } else {
173 0 : $code.= " <option value='$key'>$option</option>\n";
174 : }
175 0 : }
176 0 : }
177 0 : $code.= "</select></p></td>\n";
178 0 : $code.= "<td class='$style'>\n";
179 0 : $code.= Utilities::helpCode($helpID);
180 0 : $code.= "</td>\n";
181 0 : $code.= "</tr>\n";
182 0 : return $code;
183 : }
184 0 : }
185 : /**
186 : *
187 : * @access public
188 : * @return void
189 : *
190 : */
191 : function getYesNoFormField($label, $name, $value = 0, $style = 'tableCell', $helpID = 0, $tabIndex = 0) {
192 0 : if ($name) {
193 : //Tabindex korrigieren
194 0 : $tabIndex = $tabIndex+1000;
195 0 : if ($value == 1) {
196 0 : $checkedYes = "checked='checked'";
197 0 : $checkedNo = "";
198 0 : } else {
199 0 : $checkedYes = "";
200 0 : $checkedNo = "checked='checked'";
201 : }
202 : //Zellenstil abgleichen
203 0 : if (is_array($style)) {
204 0 : if (isset($style[$name])) $style = $style[$name];
205 0 : else $style = $style['tableRow'];
206 0 : }
207 0 : $code = "<tr>\n";
208 0 : $code.= "<td align='right' class='$style'>$label</td>\n";
209 0 : $code.= "<td class='$style'>";
210 0 : $code.= "<p class='pForm'><input name='$name' id='".$name."0' type='radio' value='1' tabindex='$tabIndex' $checkedYes/></p><label for='".$name."0'>Ja</label>";
211 0 : $code.= "<p class='pForm'><input name='$name' id='".$name."1' type='radio' value='0' tabindex='$tabIndex' $checkedNo/></p><label for='".$name."1'>Nein</label>";
212 0 : $code.= "</td>\n";
213 0 : $code.= "<td class='$style'>\n";
214 0 : $code.= Utilities::helpCode($helpID);
215 0 : $code.= "</td>\n";
216 0 : $code.= "</tr>\n";
217 0 : return $code;
218 : }
219 0 : }
220 : /**
221 : * Erzeugt eine 3-spaltige Tabellen-Zeile in einem Formular,
222 : * mit einem Label, einer Radiobutton-Gruppe und einem Hilfelink.
223 : *
224 : * @access public
225 : * @param string $label Bezeichnung der Radiobutton-Gruppe
226 : * @param string $name Feldname der Radiobutton-Gruppe
227 : * @param string $value Aktuell anliegender Wert der Radiobutton-Gruppe (für "checked"-Property)
228 : * @param string $options Assoziatives Array (Wert/Bezeichnung) mit den einzelnen Radiobuttons
229 : * @param string $style css-Formatklassen-Bezeichnung
230 : * @param int $helpID ID des zum Feld gehörigen Hilfetextes
231 : * @param int $tabIndex Tabulator-Reihenfolgeposition des ersten Radiobutton
232 : * @param bool $addLinebreak Schalter für Zeilenumbruch zwischen den einzelnen Radiobuttons
233 : * @return string Html-Anweisung
234 : *
235 : */
236 : function getRadioGroupFormField($label, $name, $value = 0, $options, $style = 'tableCell', $helpID = 0, $tabIndex = 0, $addLinebreak = false) {
237 0 : if ($name) {
238 : //Tabindex korrigieren
239 0 : $tabIndex = $tabIndex+1000;
240 : //Zellenstil abgleichen
241 0 : if (is_array($style)) {
242 0 : if (isset($style[$name])) $style = $style[$name];
243 0 : else $style = $style['tableRow'];
244 0 : }
245 0 : $code = "<tr>\n";
246 0 : $code.= "<td align='right' class='$style'>$label</td>\n";
247 0 : $code.= "<td class='$style'>";
248 0 : foreach($options as $opVal => $opDesc) {
249 0 : if ($value == $opVal) {
250 0 : $checked = "checked='checked'";
251 0 : } else {
252 0 : $checked = "";
253 : }
254 0 : $code.= "<p class='pForm'><input name='$name' id='".$name.$opVal."' type='radio' value='".$opVal."' tabindex='".$tabIndex++."'".$checked."/></p><label for='".$name.$opVal."'>".$opDesc."</label>".($addLinebreak ? "<br/>" : "");
255 0 : }
256 0 : $code.= "</td>\n";
257 0 : $code.= "<td class='$style'>\n";
258 0 : $code.= Utilities::helpCode($helpID);
259 0 : $code.= "</td>\n";
260 0 : $code.= "</tr>\n";
261 0 : return $code;
262 : }
263 0 : }
264 : /**
265 : * Erzeugt eine 3-spaltige Tabellen-Zeile in einem Formular,
266 : * bestehend aus einem Label, einer Checkbox und einem Hilfelink.
267 : * @access public
268 : * @return void
269 : *
270 : */
271 : function getCheckboxFormField($label, $name, $checked = 0, $style = 'tableCell', $helpID = 0, $tabIndex = 0) {
272 0 : if ($name) {
273 : //Tabindex korrigieren
274 0 : $tabIndex = $tabIndex+1000;
275 : //Zellenstil abgleichen
276 0 : if (is_array($style)) {
277 0 : if (isset($style[$name])) $style = $style[$name];
278 0 : else $style = $style['tableRow'];
279 0 : }
280 : // 'checked'-Attribut erstellen
281 0 : $checkedAttr = $checked ? "checked='checked'" : "";
282 0 : $code = "<tr>\n";
283 0 : $code.= "<td align='right' class='$style'>$label</td>\n";
284 0 : $code.= "<td class='$style'>";
285 0 : $code.= "<p class='pForm'><input name='$name' id='$name' type='checkbox' value='1' tabindex='$tabIndex' $checkedAttr/></p>";
286 0 : $code.= "</td>\n";
287 0 : $code.= "<td class='$style'>\n";
288 0 : $code.= Utilities::helpCode($helpID);
289 0 : $code.= "</td>\n";
290 0 : $code.= "</tr>\n";
291 0 : return $code;
292 : }
293 0 : }
294 : /**
295 : *
296 : * @access public
297 : * @return void
298 : *
299 : */
300 : function getFormSubHeader($label, $style, $colspan) {
301 0 : $code = "<tr>\n";
302 0 : $code.= "<td colspan='$colspan' class='$style'>\n";
303 0 : $code.= $label;
304 0 : $code.= "</td>\n";
305 0 : $code.= "</tr>\n";
306 0 : return $code;
307 : }
308 : /**
309 : *
310 : * @access public
311 : * @return void
312 : *
313 : */
314 : function getSmallInfoText($text, $style) {
315 0 : $code = "<tr>\n";
316 0 : $code.= "<td class='$style'></td>\n";
317 0 : $code.= "<td class='$style'>\n";
318 0 : $code.= $text;
319 0 : $code.= "</td>\n";
320 0 : $code.= "<td class='$style'></td>\n";
321 0 : $code.= "</tr>\n";
322 0 : return $code;
323 : }
324 : /**
325 : * Stellt Anzeige-Optionen in einem Array zur Verfügung
326 : *
327 : * @access public
328 : * @static
329 : * @return array mögliche Anzeige-Optionen
330 : */
331 : function getShowOptions($userID = 0) {
332 0 : global $db, $EZSQL_ERROR;
333 0 : $showOptions = array(0 => 'Nicht anzeigen', 1 => 'Portalweit anzeigen', 2 => 'Nur in Kursen anzeigen');
334 0 : if ($userID) {
335 0 : $courses = $db->get_results("SELECT courses.ID as courseID, courses.ShortName AS shortname
336 : FROM courses, user_course
337 0 : WHERE user_course.userID = ".$userID."
338 : AND user_course.courseID = courses.ID AND closed=0
339 0 : ORDER BY shortname");
340 0 : if ($courses) {
341 0 : foreach($courses as $course) {
342 0 : $i = -$course->courseID;
343 0 : $showOptions[$i] = "Nur in Kurs "".Data::toHTML($course->shortname, false) ."" anzeigen";
344 0 : }
345 0 : }
346 0 : }
347 0 : return $showOptions;
348 : }
349 : /**
350 : * Holt alle Länder aus der Datenbank und bereitet sie in einem Array auf
351 : *
352 : * @access public
353 : * @static
354 : * @return array Alle Datensätze aus "countries" oder "Keine daten" gefunden.
355 : */
356 : function getCountryOptions() {
357 0 : global $db, $EZSQL_ERROR;
358 0 : $countries = $db->get_results("SELECT countries.countryID as countryID,
359 : countries.country AS country
360 : FROM countries
361 0 : ORDER BY countries.country asc");
362 0 : if ($countries) {
363 0 : $countryOptions = array(0 => 'Bitte wählen');
364 0 : foreach($countries as $country) {
365 0 : $countryOptions[$country->countryID] = $country->country;
366 0 : }
367 0 : } else {
368 0 : $countryOptions = array(0 => 'Keine Daten');
369 : }
370 0 : return ($countryOptions);
371 : }
372 : /**
373 : * Gibt den img-Tag zur Anzeige eines Bildes zurück, das auf eine maximale Höhe
374 : * und Breite von 120 Pixeln skaliert ist.
375 : *
376 : * @access public
377 : * @param string $path Pfad zur Datei
378 : * @param string $file Dateiname
379 : * @return mixed img-Tag oder false bei falschen Parametern
380 : */
381 : function getPictureTag($path, $file) {
382 0 : if ($path && $file) {
383 0 : $picPath = $path.$file;
384 0 : $maxWidth = 120;
385 0 : $maxHeight = 120;
386 0 : $is_picture = is_file($picPath);
387 0 : if ($is_picture) {
388 0 : $picPath = rawurlencode($picPath);
389 0 : $picPath = PATH_TO_ROOT."common/makethumb.php?picurl=$picPath&maxh=$maxHeight&maxw=$maxWidth";
390 0 : $pictureTag = "<img src='".$picPath."' alt='".$file."' />";
391 0 : return $pictureTag;
392 : } else {
393 0 : return false;
394 : }
395 : } else {
396 0 : return false;
397 : }
398 : }
399 : /**
400 : * Werte, die zum Speichern in der DB entweder NULL oder ein String in
401 : * Hochkomma sein können, sollten mit dieser Funktion vor dem Einfügen in
402 : * den SQL-String maskiert werden. Es wird dazu die toMysql()-Methode der
403 : * Data-Klasse verwendent.
404 : *
405 : * @access public
406 : * @author Clemens Weiß <clemens.weiss@mni.fh-giessen.de>
407 : * @param string $value Zu maskierender Wert
408 : * @return string maskierter Wert
409 : */
410 : function nonNullValuesToMySQL($value) {
411 0 : global $db;
412 0 : if ($value[0] != "'") {
413 0 : return $value;
414 : } else {
415 0 : return "'".Data::toMysql(UserTools::stripSingleSlashes($value), false) ."'";
416 : }
417 : }
418 : /**
419 : * Gibt Namen der Benutzergruppe eines Benutzers im Kurs/Foyer zurück
420 : *
421 : * @access public
422 : * @author Benjamin Stadin <benjamin.stadin@mni.fh-giessen.de>
423 : * @param int $userID
424 : * @param int $courseID
425 : * @return string Name der Benutzergruppe
426 : */
427 : function getUserGroupName($userID, $courseID) {
428 : // Letzten Autor mit Benutzergruppe angeben
429 0 : global $db;
430 0 : if ($courseID > 0) {
431 : // Prüfen, ob Benutzer in Kurs eine andere Benutzergruppe hat. Ansonsten globale Benutzergruppe verwenden
432 0 : $groupID = $db->get_var("SELECT IF(differentUsergroup>0, differentUsergroup, user.usergroup) AS Usergroup FROM user_course JOIN user ON (id=userID) WHERE userID='$userID' AND courseID = '$courseID'");
433 0 : $groupName = $db->get_var("SELECT usergroups.name AS groupname FROM usergroups WHERE ID = '$groupID'");
434 0 : if ($groupName == null || strlen($groupName) <= 0) {
435 : // Benutzer ist nicht Mitglied im Kurs (Admin)
436 0 : $groupID = $db->get_var("SELECT usergroup FROM user WHERE ID='$userID'");
437 0 : $groupName = $db->get_var("SELECT usergroups.name AS groupname FROM usergroups WHERE ID = '$groupID'");
438 0 : if ($groupName == null || strlen($groupName) <= 0)
439 0 : $groupName = "abgemeldet";
440 0 : }
441 0 : }
442 : else {
443 : // Kurs = 0 (Foyer)
444 0 : $groupID = $db->get_var("SELECT usergroup FROM user WHERE ID='$userID'");
445 0 : $groupName = $db->get_var("SELECT usergroups.name AS groupname FROM usergroups WHERE ID = '$groupID'");
446 0 : if ($groupName == null || strlen($groupName) <= 0)
447 0 : $groupName = "abgemeldet";
448 : }
449 : // Haengt an Gruppenbezeichnung noch eine Bezeichnung an, ob externer oder interner Benutzer
450 0 : $groupName .= UserTools::getExternalInternal($userID);
451 0 : return $groupName;
452 : }
453 :
454 : /**
455 : * Haengt an Benutzernamen ein (extern) an, sodass externe Benutzer
456 : * als solche im Portal identifizierbar sind. Externe Benutzer
457 : * werden als solche bezeichnet, die keine "interne Email-Adresse" haben
458 : * (s. Portaleinstellung).
459 : *
460 : * @access public
461 : * @author Benjamin Stadin <benjamin.stadin@mni.fh-giessen.de>
462 : * @param int $userID
463 : * @param int $courseID
464 : * @return string Name der Benutzergruppe
465 : */
466 : function getExternalInternal($userID, $useBrackets = false) {
467 : // Letzten Autor mit Benutzergruppe angeben
468 0 : global $db, $settings;
469 0 : if ($settings['internal_emails'])
470 0 : $internalEmails = $settings['internal_emails'];
471 : else
472 0 : $internalEmails = null;
473 0 : $email = $db->get_var("SELECT email FROM user WHERE ID='$userID'");
474 0 : if ($email == null)
475 0 : $email = "";
476 0 : $email = strtolower($email);
477 0 : if (is_array($internalEmails)) {
478 0 : $checkAnz = count($internalEmails);
479 0 : $internal = false;
480 0 : $internalSubstring = "";
481 0 : for ($i = 0 ; $i < $checkAnz ; $i++) {
482 0 : $internalSubstring = strtolower($internalEmails[$i]);
483 0 : if (substr($email, -strlen($internalSubstring)) === $internalSubstring) {
484 0 : $internal = true;
485 0 : break;
486 : }
487 0 : }
488 0 : if (!$internal) {
489 : if ($useBrackets)
490 0 : return " (extern)";
491 : else
492 0 : return ", extern";
493 : }
494 0 : }
495 :
496 0 : return "";
497 : }
498 : }
|